<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\model;
use think\facade\Db;
use think\facade\Log;
class ShopProduct
{
	//减关联库存
	public static function declinkstock($proid,$ggid,$num){
		$splitlist = Db::name('shop_ggsplit')->where('proid',$proid)->where("ggid1={$ggid} or ggid2={$ggid}")->select()->toArray();
		if(!$splitlist) return;
		$guige = Db::name('shop_guige')->where('id',$ggid)->find();
		foreach($splitlist as $k=>$v){
			if($v['ggid1'] == $ggid){
				$thisnum = $num * $v['multiple'];
				Db::name('shop_guige')->where('id',$v['ggid2'])->update(['stock'=>Db::raw("IF(stock>={$thisnum},stock-{$thisnum},0)"),'sales'=>Db::raw("sales+$thisnum")]);
			}
			if($v['ggid2'] == $ggid){
				//$thisnum = ceil($num / $v['multiple']);
				$thisStock = floor($guige['stock'] / $v['multiple']);
				$thisSales = floor($guige['sales'] / $v['multiple']);
				Db::name('shop_guige')->where('id',$v['ggid1'])->update(['stock'=>$thisStock,'sales'=>$thisSales]);
			}
		}
		self::calculateStock($proid);
	}
	//加关联库存
	public static function addlinkstock($proid,$ggid,$num){
		$splitlist = Db::name('shop_ggsplit')->where('proid',$proid)->where("ggid1={$ggid} or ggid2={$ggid}")->select()->toArray();
		if(!$splitlist) return;
		$guige = Db::name('shop_guige')->where('id',$ggid)->find();
		foreach($splitlist as $k=>$v){
			if($v['ggid1'] == $ggid){
				$thisnum = $num * $v['multiple'];
				Db::name('shop_guige')->where('id',$v['ggid2'])->update(['stock'=>Db::raw("stock+$thisnum"),'sales'=>Db::raw("sales-$thisnum")]);
			}
			if($v['ggid2'] == $ggid){
				$thisStock = floor($guige['stock'] / $v['multiple']);
				$thisSales = floor($guige['sales'] / $v['multiple']);
				Db::name('shop_guige')->where('id',$v['ggid1'])->update(['stock'=>$thisStock,'sales'=>$thisSales]);
			}
		}
		self::calculateStock($proid);
	}
	//计算商品总库存
	public static function calculateStock($proid){
		$ggids = Db::name('shop_ggsplit')->where('proid',$proid)->column('ggid1');
		if($ggids){
			$totalstock = Db::name('shop_guige')->where('proid',$proid)->where('id','not in',$ggids)->sum('stock');
			$totalsales = Db::name('shop_guige')->where('proid',$proid)->where('id','not in',$ggids)->sum('sales');
			Db::name('shop_product')->where('id',$proid)->update(['stock'=>$totalstock,'sales'=>$totalsales]);
		}
	}
	//验证关联库存
	public static function checkstock($prolist){
		$ggidnums = [];
		foreach($prolist as $v){
			$ggid = $v['guige']['id'];
			$linkggArr = Db::name('shop_ggsplit')->where('ggid1',$ggid)->select()->toArray();
			if($linkggArr){
				foreach($linkggArr as $linkgg){
					$ggid2 = $linkgg['ggid2'];
					$num = $v['num'] * $linkgg['multiple'];
					if($ggidnums[$ggid2]){
						$ggidnums[$ggid2] += $num;
					}else{
						$ggidnums[$ggid2] = $num;
					}
				}
			}else{
				if($ggidnums[$ggid]){
					$ggidnums[$ggid] += $v['num'];
				}else{
					$ggidnums[$ggid] = $v['num'];
				}
			}
		}
		foreach($ggidnums as $ggid=>$num){
			$guige = Db::name('shop_guige')->where('id',$ggid)->find();
			if($guige['stock'] < $num) return ['status'=>0,'msg'=>'库存不足'];
		}
		return ['status'=>1];
	}
}